home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 8.2 KB | 339 lines |
- package symantec.itools.awt;
-
-
- import java.awt.Canvas;
- import java.awt.Graphics;
- import java.awt.FontMetrics;
- import java.awt.Dimension;
- import java.awt.Label;
-
-
- /**
- * Wrapping label. Displays text on one or more lines, wrapping text
- * as needed to fit in the available horizontal space.
- *
- * @version 1.0, Nov 26, 1996
- *
- * @author Symantec
- */
-
- public class WrappingLabel
- extends Canvas
- implements AlignStyle
- {
- //--------------------------------------------------
- // constants
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // class variables
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // member variables
- //--------------------------------------------------
-
- /**
- * The text string being displayed.
- */
- protected String text;
-
- /**
- * Current text alignment.
- * @see symantec.itools.awt.AlignStyle
- */
- protected int align;
-
- /**
- * Maximum ascent of the font used to display text.
- *
- */
- protected int baseline;
-
- /**
- * Metrics of the font used to display text.
- */
- protected FontMetrics fm;
-
-
- //--------------------------------------------------
- // constructors
- //--------------------------------------------------
-
- /**
- * Create default wrapping label. Default values are an empty text string
- * and left alignment.
- */
- public WrappingLabel()
- {
- this("");
- }
-
- /**
- * Create a wrapping label that displays the specified string.
- * The label will default to left alignment.
- *
- * @param s string to be displayed in label
- */
-
- public WrappingLabel(String s)
- {
- this(s, WrappingLabel.ALIGN_LEFT);
- }
- /**
- * Create wrapping label with specified text and alignment.
- *
- * @param s string to be displayed in label
- * @param a alignment, one of ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- *
- * @see symantec.itools.awt.AlignStyle
- */
-
- public WrappingLabel(String s, int a)
- {
- setText(s);
- setAlignStyle(a);
- }
-
-
- //--------------------------------------------------
- // accessor members
- //--------------------------------------------------
-
- /**
- * Gets the current text alignment setting.
- *
- * @return the current alignment, one of ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @see #setAlignStyle
- * @see symantec.itools.awt.AlignStyle
- */
- public int getAlignStyle()
- {
- return align;
- }
-
- /**
- * Sets current text alignment.
- *
- * @param an alignment style, one of ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @see #getAlignStyle
- * @see symantec.itools.awt.AlignStyle
- */
-
- public void setAlignStyle(int a)
- {
- align = a;
- invalidate();
- }
-
- /**
- * Gets the current label text.
- *
- * @return the label's text string
- * @see #setText
- */
- public String getText()
- {
- return text;
- }
-
- /**
- * Sets the label text.
- *
- * @param s the new label text
- *
- * @see #getText
- */
- public void setText(String s)
- {
- text = s;
- repaint();
- }
-
- //--------------------------------------------------
- // event methods
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // class methods
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // member methods
- //--------------------------------------------------
-
- /**
- * Returns an empty string.
- * This is a standard Java AWT method which typically returns a string
- * representing the state of this object.
- *
- * @return the empty string
- */
- public String paramString()
- {
- return "";
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- if (text != null)
- {
- int x, y;
- int boundx, boundy;
- Dimension d;
- int fromIndex = 0;
- int pos = 0;
- int bestpos;
- String largestString;
- String s;
-
- // Set up some class variables
- fm = getToolkit().getFontMetrics(getFont());
- baseline = fm.getMaxAscent();
-
- // Get the maximum height and width of the current control
- d = size();
- boundx = d.width;
- boundy = d.height;
-
- // X and Y represent the coordinates of the upper left portion
- // of the next text line.
- x = 0;
- y = 0;
-
- // While we haven't passed the bottom of the label and we
- // haven't run past the end of the string...
- while ((y + fm.getHeight()) <= boundy && fromIndex != -1)
- {
- // Automatically skip any spaces at the beginning of the line
- while (fromIndex < text.length() && text.charAt(fromIndex) == ' ')
- {
- ++fromIndex;
- // If we hit the end of line while skipping spaces, we're done.
- if (fromIndex >= text.length()) break;
- }
-
- // fromIndex represents the beginning of the line
- pos = fromIndex;
- bestpos = -1;
- largestString = null;
-
- while (pos >= fromIndex)
- {
- pos = text.indexOf(' ', pos);
-
- // Couldn't find another space?
- if (pos == -1)
- {
- s = text.substring(fromIndex);
- }
- else
- {
- s = text.substring(fromIndex, pos);
- }
-
- // If the string fits, keep track of it.
- if (fm.stringWidth(s) < boundx)
- {
- largestString = s;
- bestpos = pos;
-
- // If we've hit the end of the string, use it.
- if (pos == -1) break;
- }
- else
- {
- break;
- }
-
- ++pos;
- }
-
- if (largestString == null)
- {
- // Couldn't wrap at a space, so find the largest line
- // that fits and print that. Note that this will be
- // slightly off -- the width of a string will not necessarily
- // be the sum of the width of its characters, due to kerning.
- int totalWidth = 0;
- int oneCharWidth = 0;
-
- pos = fromIndex;
-
- while (pos < text.length())
- {
- oneCharWidth = fm.charWidth(text.charAt(pos));
- if ((totalWidth + oneCharWidth) >= boundx) break;
- totalWidth += oneCharWidth;
- ++pos;
- }
-
- drawAlignedString(g, text.substring(fromIndex, pos), x, y, boundx);
- fromIndex = pos;
- }
- else
- {
- drawAlignedString(g, largestString, x, y, boundx);
-
- fromIndex = bestpos;
- }
-
- y += fm.getHeight();
- }
-
- // We're done with the font metrics...
- fm = null;
- }
- }
-
- /**
- * This helper method draws a string aligned the requested way.
- * @param g the graphics context used for painting
- * @param s the string to draw
- * @param x the point to start drawing from, x coordinate
- * @param y the point to start drawing from, y coordinate
- * @param width the width of the area to draw in, in pixels
- */
- protected void drawAlignedString(Graphics g, String s, int x, int y, int width)
- {
- int drawx;
- int drawy;
-
- drawx = x;
- drawy = y + baseline;
-
- if (align != WrappingLabel.ALIGN_LEFT)
- {
- int sw;
-
- sw = fm.stringWidth(s);
-
- if (align == WrappingLabel.ALIGN_CENTERED)
- {
- drawx += (width - sw) / 2;
- }
- else if (align == WrappingLabel.ALIGN_RIGHT)
- {
- drawx = drawx + width - sw;
- }
- }
-
- g.drawString(s, drawx, drawy);
- }
- }